<?php
 	include_once('db/constants.php');
 	include_once('Mobile_Detect.php');

	// Facebook PHP SDK v4.0.11

	// include required files form Facebook SDK

	require_once( 'Facebook/HttpClients/FacebookHttpable.php' );
	require_once( 'Facebook/HttpClients/FacebookCurl.php' );
	require_once( 'Facebook/HttpClients/FacebookCurlHttpClient.php' );

	require_once( 'Facebook/Entities/AccessToken.php' );
	require_once( 'Facebook/Entities/SignedRequest.php' );

	require_once( 'Facebook/FacebookSession.php' );
	require_once( 'Facebook/FacebookRedirectLoginHelper.php' );
	require_once( 'Facebook/FacebookRequest.php' );
	require_once( 'Facebook/FacebookResponse.php' );
	require_once( 'Facebook/FacebookSDKException.php' );
	require_once( 'Facebook/FacebookRequestException.php' );
	require_once( 'Facebook/FacebookOtherException.php' );
	require_once( 'Facebook/FacebookAuthorizationException.php' );
	require_once( 'Facebook/GraphObject.php' );
	require_once( 'Facebook/GraphSessionInfo.php' );

	use Facebook\HttpClients\FacebookHttpable;
	use Facebook\HttpClients\FacebookCurl;
	use Facebook\HttpClients\FacebookCurlHttpClient;

	use Facebook\Entities\AccessToken;
	use Facebook\Entities\SignedRequest;

	use Facebook\FacebookSession;
	use Facebook\FacebookRedirectLoginHelper;
	use Facebook\FacebookRequest;
	use Facebook\FacebookResponse;
	use Facebook\FacebookSDKException;
	use Facebook\FacebookRequestException;
	use Facebook\FacebookOtherException;
	use Facebook\FacebookAuthorizationException;
	use Facebook\GraphObject;
	use Facebook\GraphSessionInfo;

	// IE compatible
	header('P3P: CP="NOI ADM DEV PSAi NAV OUR STP IND DEM"; charset=utf-8');

	// start session
	session_start();

	// init app with app id and secret
	FacebookSession::setDefaultApplication( APP_ID, SECRET );

	// login helper with redirect_uri
	$detect = new Mobile_Detect;
	if ($detect->isMobile())
		$helper = new FacebookRedirectLoginHelper( SERVER_WEB );
	else
		$helper = new FacebookRedirectLoginHelper( APP_CANVAS );

	// see if a existing session exists
	if ( isset( $_SESSION ) && isset( $_SESSION['fb_token'] ) ) {
		// create new session from saved access_token
		$session = new FacebookSession( $_SESSION['fb_token'] );

		// validate the access_token to make sure it's still valid
		try {
			if ( !$session->validate() ) {
				$session = null;
			}
		} catch ( Exception $e ) {
			// catch any exceptions
			$session = null;
		}
	}  

	if ( !isset( $session ) || $session === null ) {
		// no session exists

		try {
			$session = $helper->getSessionFromRedirect();
		} catch( FacebookRequestException $ex ) {
			// When Facebook returns an error
			// handle this better in production code
			print_r( $ex );
		} catch( Exception $ex ) {
			// When validation fails or other local issues
			// handle this better in production code
			print_r( $ex );
		}

	}

	// see if we have a session
	if ( isset( $session ) ) {

		// save the session
		$_SESSION['fb_token'] = $session->getToken();
		// create a session using saved token or the new one we generated at login
		$session = new FacebookSession( $session->getToken() );

		// graph api request for user data
		$request = new FacebookRequest( $session, 'GET', '/me' );
		$response = $request->execute();
		// get response
		$user_profile = $response->getGraphObject()->asArray();
		
		//include_once('lenguages_loader.php');

		$access_token = $_SESSION['fb_token'];
		
	} else {
		$url =$helper->getLoginUrl( array( /*'email', 'user_friends'*/ ) );

		echo '<script>top.location.href="'.$url.'";</script>';
		//header('Location: '.$url);
		exit();
	}

	include_once('lenguages_loader.php');
?>